package com.dr.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.dr.common.Const;
import com.dr.pojo.ConstantItem;
import com.dr.pojo.Dept;
import com.dr.pojo.RegLevel;
import com.dr.pojo.User;
import com.dr.service.ConsItemService;
import com.dr.service.DeptService;
import com.dr.service.RegLevelService;
import com.dr.service.UserService;

@Controller
@RequestMapping("/um")
public class UserController {
	@Autowired
	private UserService userService;//	Spring注入ServiceImpl的实例
	@Autowired
	private DeptService deptService;
	@Autowired
	private RegLevelService regLevelService;
	@Autowired
	private ConsItemService consItemService;
	
	/* 
	 * 	显示用户列表页面，加载所有用户信息
	 */
	@RequestMapping("/userList")
	public ModelAndView userList(User user) {
		List<User> userList = userService.findUserList(); //	取得用户列表数据	
				
		ModelAndView mav = new ModelAndView();
		mav.addObject("userList", userList);  //	把用户数据加载到页面中	
		mav.setViewName("userManage/userList");
		
		return mav;
	}
	
//	/*  
//	 * 	根据页面输入的内容查询用户信息
//	 */
//	@RequestMapping("/userListByName")
//	public ModelAndView userListByName(String txtQuery) {
//		List<User> userList = userService.findUserByName(txtQuery); //	根据页面输入的值取得用户数据		
//		
//		ModelAndView mav = new ModelAndView();
//		mav.addObject("userList", userList);
//		mav.addObject("txtQuery", txtQuery);
//		mav.setViewName("userManage/userList.jsp");
//		
//		return mav;
//	}
//	
	/*  
	 * 	打开新增用户页面
	 */
	@RequestMapping("/toAdd")
	public ModelAndView toAdd() {
		List<Dept> deptList = deptService.findDeptList();
		List<RegLevel> regLevList = regLevelService.findRegLevelList();
		List<ConstantItem> consItemList = consItemService.findConsItemList();
		
		ModelAndView mav = new ModelAndView();
		mav.setViewName("userManage/member-add");
		mav.addObject("deptList", deptList);
		mav.addObject("regLevList", regLevList);
		mav.addObject("consItemList", consItemList);
		
		return mav; 
	}
	
	/*  
	 * 	执行新增用户
	 */
	@ResponseBody
	@RequestMapping("/doAdd")
	public Map<String,Object> doAdd(User user, HttpSession session) {
		User addUser = userService.findUserByUserName(user.getUserName());
		System.out.print(user);
		Map<String,Object> map = new HashMap<>();
		
		//	判断待新增用户是否存在
		if(addUser != null) {
			//	如果存在则提示账户重复，返回到新增页面
			map.put("status", Const.STATUS_FAILED);		// 	返回标记
			map.put("attrInfoUserAdd", Const.INF_USER_NAME_EXIST); //	把用户数据加载到页面中
			return map;
		}else {			
			int result = userService.insertUser(user);
			if(result > 0) {
				map.put("status", Const.STATUS_SCUESS);		// 	返回标记
				map.put("attrInfoUserAdd", "增加用户成功！");
			}else {
				map.put("status", Const.STATUS_FAILED);		// 	返回标记
				map.put("attrInfoUserAdd", "插入数据失败！");
			}
			return map;
		}				
	}
	
	/*  
	 * 	打开修改用户页面
	 */
	@RequestMapping("/toUpdate")
	public ModelAndView toUpdate(Integer id) {
		User user = userService.findUserById(id);
		
		List<Dept> deptList = deptService.findDeptList();
		List<RegLevel> regLevList = regLevelService.findRegLevelList();
		List<ConstantItem> consItemList = consItemService.findConsItemList();
		
		ModelAndView mav = new ModelAndView();
		mav.setViewName("userManage/member-update");
		mav.addObject("user", user);
		mav.addObject("deptList", deptList);
		mav.addObject("regLevList", regLevList);
		mav.addObject("consItemList", consItemList);
		
		return mav; 
	}
	
	/*  
	 * 	执行修改用户
	 */
	@ResponseBody
	@RequestMapping("/doUpdate")
	public Map<String, Object> doUpdate(User user) {							
		Map<String,Object> map = new HashMap<>();
		System.out.println(user);
		int result = userService.updateUser(user);
		System.out.println("result="+result);
		if (result >0) {
			map.put("status", Const.STATUS_SCUESS);		// 	返回标记
			map.put("attrInfoUserAdd", "修改用户成功！");
		}else {
			map.put("status", Const.STATUS_FAILED);		// 	返回标记
			map.put("attrInfoUserAdd", "修改数据失败！");
		}						
		return map;							
	}
	
	
//	/*  
//	 * 	执行修改用户
//	 */
//	@RequestMapping("/doUpdate")
//	public ModelAndView doUpdate(User user, HttpSession session) {				
//			userService.updateUser(user, session);
//			return userList(user);					
//	}
//	
//	/*  
//	 * 	执行删除用户
//	 */
//	@RequestMapping("/doDelete")
//	public ModelAndView doDelete(User user, HttpSession session) {				
//			userService.deleteUser(user, session);
//			return userList(user);					
//	}
	

	/*  
	 * 	执行删除用户
	 */
	@ResponseBody
	@RequestMapping("/doDelete")
	// [String] ... 可变参数
	public Map<String,Object> doDelete(String... ids) {	
		Map<String,Object> map = new HashMap<>();
		int result = userService.deleteUser(ids);
		if (result >0) {
			map.put("status", Const.STATUS_SCUESS);		// 	返回标记
			map.put("attrInfoUserAdd", "删除用户成功！");
		}else {
			map.put("status", Const.STATUS_FAILED);		// 	返回标记
			map.put("attrInfoUserAdd", "删除用户失败！");
		}
		return map;					
	}
	
	/*  
	 * 	打开修改密码页面
	 */
	@RequestMapping("/toPassword")
	public ModelAndView toUpdate(String userName) {
						
		ModelAndView mav = new ModelAndView();
		mav.setViewName("userManage/pwdUpdate");
		mav.addObject("userName", userName);		
		
		return mav; 
	}
	/*  
	 * 	执行修改密码
	 */
	@ResponseBody
	@RequestMapping("/doPassword")
	public Map<String, Object> doPassword(User user) {							
		Map<String,Object> map = new HashMap<>();		
		System.out.println(user.getUserName());
		int result = userService.updatePassword(user);
		System.out.println("result="+result);
		if (result >0) {
			map.put("status", Const.STATUS_SCUESS);		// 	返回标记
			map.put("attrInfoUserAdd", "修改密码成功！");
		}else {
			map.put("status", Const.STATUS_FAILED);		// 	返回标记
			map.put("attrInfoUserAdd", "修改密码失败！");
		}						
		return map;							
	}
}
